package com.concurrent.entity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @program: concurrent_project
 * @description: 动物园类
 * @author: Wednesday
 * @create: 2020-06-10 11:07
 **/
public class Zoo implements Runnable {

    private CountDownLatch latch;
    private String name;

    public Zoo(CountDownLatch latch, String name) {
        this.latch = latch;
        this.name = name;
    }

    @Override
    public void run() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
            System.out.println(sdf.format(new Date()) + "=========等待所有人到齐");
            //等待计数器为0时执行
//            latch.await();
            //不需要等待计数器为0，设定等待时间为3秒，不管计数器是否为0，都继续执行
            latch.await(3, TimeUnit.SECONDS);
            System.out.println(sdf.format(new Date()) + "=========进入" + name);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
